using UnityEngine;
using QFramework;
using System;
using Unity.VisualScripting;
using Unity.Burst.Intrinsics;

namespace ProjectSurvivor
{
	public partial class Enemy : ViewController
	{
		public float MovementSpeed = 2.0f;
		public float Hp = 3;

		void Start()
		{
			EnemyGenerator.EnemyCount.Value++;
		}
		void OnDestroy()
		{
			EnemyGenerator.EnemyCount.Value--;
		}
		private void FixedUpdate()
		{
			if (Player.Default)
			{
				var direction = (Player.Default.transform.position - transform.position).normalized;
				//transform.Translate(direction * Time.fixedDeltaTime * MovementSpeed);
				SelfRigidbody2D.velocity = direction * MovementSpeed;
			}
			else
			{
				SelfRigidbody2D.velocity = Vector2.zero;
			}
		}
		void Update()
		{
			//var player = FindObjectOfType<Player>();

			if (Hp <= 0)
			{
				//	UIKit.OpenPanel<UIGameOverPassPanel>();

				Global.GeneratePowerUp(gameObject);
				this.DestroyGameObjGracefully();
				//Global.Exp.Value++;

				//经验值的掉落

			}

		}
		private bool mIgnoreHurt = false;
		internal void Hurt(float value, bool force = false)
		{
			if (mIgnoreHurt && !force) return;
			FlotingTextController.Play(transform.position, value.ToString());
			Sprites.color = Color.red;
			AudioKit.PlaySound("Hit");
			ActionKit.Delay(0.2f, () =>
			{
				this.Hp -= value;
				this.Sprites.color = Color.white;
				mIgnoreHurt = false;
			}).Start(this);
		}
	}
}
